Skip to content

feat(runtime): rank exact matches above higher priority suggestions - #511

Open
1ilsang (1ilsang) wants to merge 2 commits into
microsoft:mainfrom
1ilsang:feat/rank-suggestions-by-match-relevance
Open

1ilsang (1ilsang) wants to merge 2 commits into
microsoft:mainfrom
1ilsang:feat/rank-suggestions-by-match-relevance

Conversation

@1ilsang

Copy link
Copy Markdown
ASIS TOBE
asis tobe-rank
asis tobe-l

Summary

Typing a suggestion's full name did not always select it. Suggestions are sorted by priority first, so a higher priority partial match stayed on top of an exact match.

The git spec gives the current branch priority 100 and other branches priority 75. With the current branch feat/foo-1234 and another branch feat/foo-:

$ git checkout feat/foo-
before: ⭐ feat/foo-1234   (current branch, 100)
          feat/foo-        (75)
after:    feat/foo-        (75, exact match)
        ⭐ feat/foo-1234   (100)
  • Suggestions whose name, or any of their aliases, exactly matches the typed text now sort first. The remaining suggestions keep the existing priority order.
  • The match is case-sensitive, since options such as -l and -L are different.
  • Tokens containing a path separator are treated as paths and matched by their basename (foo-), so the full token (feat/foo-) is compared too. Without this, branch names would never match exactly.

@1ilsang

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@cpendery cpendery (cpendery) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall looks good! left a few small tweaks / nits

Comment thread src/runtime/suggestion.ts
.map((s) => {
if (s.name == null) return;
if (s.name instanceof Array) {
const matchedName = s.name.find((n) => n.toLowerCase().startsWith(normalizedPartialCmd));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if we are trying to prioritize exact matches, we should also change this condition and below to favor exact matches first, then prefix driven matches

Comment thread src/runtime/suggestion.ts
}
: undefined;
}
return s.name.toLowerCase().startsWith(normalizedPartialCmd)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: same as above

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { getArgDrivenRecommendation, getSubcommandDrivenRecommendation } from "../../runtime/suggestion.js";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe instead of adding this suggestion.test.ts, we can just add some cases to testData in runtime.test.ts to cover it?

Comment thread src/runtime/suggestion.ts
// (-l vs -L) and git refs differ by case. Tokens containing a path separator are treated as paths and
// reduced to their basename (partialCmd). File and folder suggestions are named by their basename, so
// they compare against it; every other suggestion (e.g. the git branch feat/foo) is named in full and
// compares against the full token, so typing feat/foo does not match an unrelated branch named foo.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: trying to keep comments to a minimum in the repo, this function looks pretty self explanatory, no need to add it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants